var express = require('express');
var router = express.Router();
const db = require('../config/db');
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/contentList', (req, res, next) => {
  // sql查询user表
  db.query('select * from my_content', [], function (results, fields) {
    // 以json的形式返回
    res.json({ results })
  })
});

// 发表评论
router.post('/create_content', function (req, res, next) {
  console.log(req.body);
  const sqlStr1 = "insert into my_content(content,avatar,create_time,update_time,nickname,user_id) values (?,?,?,?,?)"
  db.query(sqlStr1, [req.body.content, req.body.avatar, new Date(),new Date(),req.body.nickname,user_id], (err, resulys) => {
    if (err) return console.log(err)

    // 成功返回数据
    if (resulys.affectedRows === 1) console.log("成功")
    // res.send()方法发送数据到页面显示
  })
  res.send({ "msg": "成功" });
});

module.exports = router;
